* file: cces_regressions.do
* Description: Analysis from Fraga, Moskowitz and Schneer for sections of paper using CCES Data

// ssc install binscatter

********************************
*** panel 2010-2014 analysis ***
********************************
cd "~/Desktop/replication"

set more 1

*** read in the cces data
use "CCES_Panel_Full3waves_VV_V3.dta", clear


*** identify if respondent is moving by examining state and district ***
* are they in the same state for 2010-14?
gen _same_state=0
replace _same_state=1 if inputstate_10==inputstate_post_10 & ///
	inputstate_10==inputstate_12 & inputstate_10==inputstate_post_12 & /// 
	inputstate_10==inputstate_14 & inputstate_10==inputstate_post_14 

* clean up district codings
destring cdid*, replace
replace cdid112_10 = 1 if cdid112_10==0 /* re-code at-large =1 for 2010 variables*/
replace cdid112_post_10 = 1 if cdid112_post_10==0 /* re-code at-large =1 */

* same district within redistricting cycles
gen _same_dist=0
replace _same_dist=1 if cdid112_10==cdid112_post_10 & ///
	cdid112_10==cdid112_12 & cdid112_10==cdid112_post_12 & ///
	cdid113_12==cdid113_post_12  & ///
	cdid113_12==cdid114_14 & cdid113_12==cdid114_post_14 

*** restrict to non-movers
keep if _same_state==1 & _same_dist==1

*** rename district variables for later reshaping (transposing) of data
gen _cd_10=cdid112_10
gen _cd_12=cdid113_12
gen _cd_14=cdid114_14


*** contacted by campaign
foreach x in 10 12 14 {
* any contact
gen _any_contact_`x'=1 if CC`x'_425a==1
replace _any_contact_`x'=0 if CC`x'_425a==2
* in person
gen _inperson_contact_`x'=0 if !missing(_any_contact_`x')
replace _inperson_contact_`x'=1 if CC`x'_425b_1==1
* phone
gen _phone_contact_`x'=0 if !missing(_any_contact_`x')
replace _phone_contact_`x'=1 if CC`x'_425b_2==1
* email or text
gen _emailtext_contact_`x'=0 if !missing(_any_contact_`x')
replace _emailtext_contact_`x'=1 if CC`x'_425b_3==1
* postcard or mail
gen _mail_contact_`x'=0 if !missing(_any_contact_`x')
replace _mail_contact_`x'=1 if CC`x'_425b_4==1

gen _methods_of_contact_`x' = 0 if !missing(_any_contact_`x')
foreach var of varlist CC`x'_425b_1 CC`x'_425b_2 CC`x'_425b_3 CC`x'_425b_4 {
	replace _methods_of_contact_`x' = _methods_of_contact_`x' + 1 if `var'==1
}
}
*** whether or not they can evaluate traits of House candidate
** competence
* 2010
gen _not_sure_D_comp_10 = 0 if !missing(CC10_335a1)
replace _not_sure_D_comp_10 = 1 if CC10_335a1==8
gen _not_sure_R_comp_10 = 0 if !missing(CC10_335b1)
replace _not_sure_R_comp_10 = 1 if CC10_335b1==8
* 2012 
gen _not_sure_D_comp_12 = 0 if !missing(CC12_342a1)
replace _not_sure_D_comp_12 = 1 if CC12_342a1==8
gen _not_sure_R_comp_12 = 0 if !missing(CC12_342b1)
replace _not_sure_R_comp_12 = 1 if CC12_342b1==8
* 2014 
gen _not_sure_D_comp_14 = 0 if !missing(CC14_342a1)
replace _not_sure_D_comp_14 = 1 if CC14_342a1==8
gen _not_sure_R_comp_14 = 0 if !missing(CC14_342b1)
replace _not_sure_R_comp_14 = 1 if CC14_342b1==8
** integrity
* 2010
gen _not_sure_D_integ_10 = 0 if !missing(CC10_335a2)
replace _not_sure_D_integ_10 = 1 if CC10_335a2==8
gen _not_sure_R_integ_10 = 0 if !missing(CC10_335b2)
replace _not_sure_R_integ_10 = 1 if CC10_335b2==8
* 2012 
gen _not_sure_D_integ_12 = 0 if !missing(CC12_342a2)
replace _not_sure_D_integ_12 = 1 if CC12_342a2==8
gen _not_sure_R_integ_12 = 0 if !missing(CC12_342b2)
replace _not_sure_R_integ_12 = 1 if CC12_342b2==8
* 2014 
gen _not_sure_D_integ_14 = 0 if !missing(CC14_342a2)
replace _not_sure_D_integ_14 = 1 if CC14_342a2==8
gen _not_sure_R_integ_14 = 0 if !missing(CC14_342b2)
replace _not_sure_R_integ_14 = 1 if CC14_342b2==8
** ideology
foreach x in 10 12 14 {
	gen _not_sure_D_ideo_`x' = 0 if !missing(CC`x'_341K)
	replace _not_sure_D_ideo_`x' = 1 if CC`x'_341K==8
	gen _not_sure_R_ideo_`x' = 0 if !missing(CC`x'_341L)
	replace _not_sure_R_ideo_`x' = 1 if CC`x'_341L==8
}


*** How would you describe the new Congressional District you live in?
*** 1. Most people are Democratic
*** 2. Most people are Republican
*** 3. My district is a mix with no single dominant party
*** Variables CC12 388b and (only asked in 2012 and 2014)
foreach x in 12 14 {
	gen _dist_mixDsRs_`x' = CC`x'_388b
}

* merge in st abbreviations
* only do so for obs with the same pre- and post-state (both non-missing
gen st_fips = inputstate_10
tostring st_fips, replace
replace st_fips = "0" + st_fips if strlen(st_fips)==1
merge m:1 st_fips using "st_abbrev_fips.dta"
rename state st_abbrev
drop if _merge==2
drop _merge

*** keep only necessary variables
rename caseid _caseid
keep _* pid7_* st_abbrev weight 

*** ensure a full panel
gen fullbalance = 1 if !missing(_any_contact_10) & !missing(_any_contact_14)

*** reshape from wide to long
reshape long _cd_ _any_contact_ _inperson_contact_ _phone_contact_ _mail_contact_ /// 
_emailtext_contact_ _methods_of_contact_ _dist_mixDsRs_ pid7_ ///
_not_sure_D_comp_ _not_sure_R_comp_ _not_sure_D_integ_ _not_sure_R_integ_ ///
_not_sure_D_ideo_ _not_sure_R_ideo_, i(_caseid) j(year)
replace year = year + 2000
rename _cd_ district

* merge in competitiveness measures for 113th congress
merge m:1 st_abbrev district year using "district_turnout_characteristics.dta", ///
	keepusing(st_abbrev district year _pvi uncontested twoDs_or_twoRs)
drop if _merge==2
tab st_abbrev if _merge==1
drop if _merge==1
drop _merge


*** create a state x year variable for state-year FEs
egen st_yr = group(st_abbrev year)

*** create cluster variable based on district in 2010 and district in 2012
gen dist_10=district if year==2010
bys _caseid (year): replace dist_10=district[_n-1] if year==2012 & year[_n-1]==2010
bys _caseid (year): replace dist_10=district[_n-2] if year==2014 & year[_n-2]==2010
gen dist_12=district if year==2012
bys _caseid (year): replace dist_12=district[_n+1] if year==2010 & year[_n+1]==2012
bys _caseid (year): replace dist_12=district[_n-1] if year==2014 & year[_n-1]==2012
egen cluster_dist=group(st_abbrev dist_10 dist_12)
bys _caseid (year): replace cluster_dist = cluster_dist[_n-1] if year==2014 & missing(cluster_dist)

*** old district fixed effect
egen old_dist = group(st_abbrev dist_10)
egen new_dist = group(st_abbrev dist_12)

*** PVI for 2010
bys _caseid (year): gen _10pvi = _pvi[1] 


*** create partisan composition variable coded =-1 if mostly Rs, =1 if mostly Ds, =0 if mix
gen _partisan_comp = 0 if _dist_mixDsR==3
replace _partisan_comp = -1 if _dist_mixDsR==2
replace _partisan_comp = 1 if _dist_mixDsR==1

*** create a variable identifying Democrats =1 and Republicans =-1
* it's based on party ID from 2010. leaners and weaks are included as Ds and Rs
* note that "true" independents are excluded
gen Dpos1_Rneg1 = 1 if inlist(pid7_,1,2,3) & year==2010
replace Dpos1_Rneg1 = -1 if inlist(pid7_,5,6,7) & year==2010
bys _caseid (year): replace Dpos1_Rneg1 = Dpos1_Rneg1[_n-1] if year==2012
bys _caseid (year): replace Dpos1_Rneg1 = Dpos1_Rneg1[_n-2] if year==2014

*** create a partisan alignment variable
local cutoff 5
gen _aligned = 0 if !missing(_pvi)
replace _aligned = 1 if _pvi<-`cutoff' & Dpos1_Rneg1==-1
replace _aligned = 1 if _pvi>`cutoff' & !missing(_pvi) & Dpos1_Rneg1==1
gen _misaligned = 0 if !missing(_pvi)
replace _misaligned = 1 if _pvi>`cutoff' & !missing(_pvi) & Dpos1_Rneg1==-1
replace _misaligned = 1 if _pvi<-`cutoff' & Dpos1_Rneg1==1
gen _comp = 0 if !missing(_aligned) & !missing(_misaligned)
replace _comp = 1 if _aligned==0 & _misaligned==0 & inlist(Dpos1_Rneg1,1,-1)

gen _partisan_index = 0 if _pvi>=-5 & _pvi<=5
replace _partisan_index = 1 if _pvi>=6 & _pvi<=10
replace _partisan_index = 2 if _pvi>=11 & _pvi<.
replace _partisan_index = -1 if _pvi<=-6 & _pvi>=-10
replace _partisan_index = -2 if _pvi<=-11 

gen aligned_pvi = Dpos1_Rneg1 * _pvi


*** can you evaluate your party's candidate
foreach x in comp integ ideo {
	gen _`x'_eval = 1 if (_not_sure_D_`x'_==0 & Dpos1_Rneg1==1) | (_not_sure_R_`x'_==0 & Dpos1_Rneg1==-1)
	replace _`x'_eval = 0 if (_not_sure_D_`x'_==1 & Dpos1_Rneg1==1) | (_not_sure_R_`x'_==1 & Dpos1_Rneg1==-1)
}


*** export individual level data set for r graphs
preserve
drop if uncontested==1 | twoDs_or_twoRs==1 | st_abbrev=="LA"
saveold "out/cces_panel_clean.dta", replace version(12)
restore

*** 2012: are voters aware of the partisan composition of their district
preserve
binscatter _partisan_comp _pvi [aw=weight] if year==2012 & uncontested==0 & twoDs_or_twoRs==0 & st_abbrev!="LA", gen(scatter_bin) nq(18)

gen wt_part_comp=weight*_partisan_comp
gen wt_pvi=weight*_pvi
bys scatter_bin: egen sum_part_comp = total(wt_part_comp)
by scatter_bin: egen sum_pvi = total(wt_pvi)
by scatter_bin: egen sum_weights=total(weight)
gen mean_pvi=sum_pvi/sum_weights
gen mean_part_comp=sum_part_comp/sum_weights
by scatter_bin: gen bin_n=_N
keep scatter_bin mean_part_comp mean_pvi bin_n sum_weights
by scatter_bin: keep if _n==1
drop if scatter_bin==.

saveold "out/binned_perceived2012.dta", replace version(12)
restore

*** regression tables for perceived district partisan composition
*Table 3
label var _pvi "PVI"
label var _10pvi "Lagged PVI"

eststo clear
eststo: reg _partisan_comp _pvi [pw=weight] if year==2012, cluster(new_dist)
	estadd local oldcd "No"
eststo: areg _partisan_comp _pvi [pw=weight] if year==2012, cluster(new_dist) absorb(old_dist)
	estadd local oldcd "Yes"
eststo: reg _partisan_comp _pvi _10pvi [pw=weight] if year==2012, cluster(new_dist) 
	estadd local oldcd "No"

esttab using "out/tab3.tex", replace nonotes label ///
	title("Perceived Partisan Composition of District | 2012" \label{cces-perceived-12}) ///
	scalars("oldcd Old District FEs") b(4) se(4) r2  nogaps nomtitles nostar ///
	addnotes("Standard errors, clustered by congressional district, are" ///
			 "in parentheses.")
			 
*** are voters aware of the partisan composition of their district
preserve
binscatter _partisan_comp _pvi [aw=weight] if year==2014 & uncontested==0 & twoDs_or_twoRs==0 & st_abbrev!="LA", gen(scatter_bin) nq(18)

gen wt_part_comp=weight*_partisan_comp
gen wt_pvi=weight*_pvi
bys scatter_bin: egen sum_part_comp = total(wt_part_comp)
by scatter_bin: egen sum_pvi = total(wt_pvi)
by scatter_bin: egen sum_weights=total(weight)
gen mean_pvi=sum_pvi/sum_weights
gen mean_part_comp=sum_part_comp/sum_weights
by scatter_bin: gen bin_n=_N
keep scatter_bin mean_part_comp mean_pvi bin_n sum_weights
by scatter_bin: keep if _n==1
drop if scatter_bin==.

saveold "out/binned_perceived2014.dta", replace version(12)
restore

*** regression tables for perceived district partisan composition
eststo clear
eststo: reg _partisan_comp _pvi [pw=weight] if year==2014, cluster(new_dist)
	estadd local oldcd "No"
eststo: areg _partisan_comp _pvi [pw=weight] if year==2014, cluster(new_dist) absorb(old_dist)
	estadd local oldcd "Yes"
eststo: reg _partisan_comp _pvi _10pvi [pw=weight] if year==2014, cluster(new_dist) 
	estadd local oldcd "No"

*Table A31
esttab using "out/tabA31.tex", replace nonotes label ///
	title("Perceived Partisan Composition of District | 2014" \label{cces-perceived-14}) ///
	scalars("oldcd Old District FEs") b(4) se(4) r2  nogaps nomtitles nostar ///
	addnotes("Standard errors, clustered by congressional district, are" ///
			 "in parentheses.") 


*** reported campaign contact ***
*** 2012: are voters reporting more contact in aligned districts
preserve
binscatter _any_contact aligned_pvi [aw=weight] if year==2012 & uncontested==0 & twoDs_or_twoRs==0 & st_abbrev!="LA", gen(scatter_bin) nq(18) 

gen wt_contact=weight*_any_contact
gen wt_pvi=weight*aligned_pvi
bys scatter_bin: egen sum_contact = total(wt_contact)
by scatter_bin: egen sum_pvi = total(wt_pvi)
by scatter_bin: egen sum_weights=total(weight)
gen mean_pvi=sum_pvi/sum_weights
gen mean_contact=sum_contact/sum_weights
by scatter_bin: gen bin_n=_N
keep scatter_bin mean_contact mean_pvi bin_n sum_weights
by scatter_bin: keep if _n==1
drop if scatter_bin==.

saveold "out/binned_contact2012.dta", replace version(12)
restore

*** reported campaign contact ***
*** 2012: are voters reporting more contact in aligned districts
preserve
binscatter _ideo_eval aligned_pvi [aw=weight] if year==2012 & uncontested==0 & twoDs_or_twoRs==0 & st_abbrev!="LA", gen(scatter_bin) nq(18) 

gen wt_eval=weight*_ideo_eval
gen wt_pvi=weight*aligned_pvi
bys scatter_bin: egen sum_eval = total(wt_eval)
by scatter_bin: egen sum_pvi = total(wt_pvi)
by scatter_bin: egen sum_weights=total(weight)
gen mean_pvi=sum_pvi/sum_weights
gen mean_eval=sum_eval/sum_weights
by scatter_bin: gen bin_n=_N
keep scatter_bin mean_eval mean_pvi bin_n sum_weights
by scatter_bin: keep if _n==1
drop if scatter_bin==.

saveold "out/binned_ideo_eval2012.dta", replace version(12)
restore

label var _aligned "Partisan-aligned district"
label var _misaligned "Partisan-misaligned district"
label var _comp "Competitive district"

xtset, clear
xtset _caseid year
set more 1
eststo clear

*** include all districts, 2010 and 2014
* drop Louisiana due to strange rules

*Table 5
drop if st_abbrev=="LA"

foreach outcome in _any_contact _inperson_contact_ _phone_contact_ _mail_contact_ _emailtext_contact_ {
	capture drop temp_fullbalance
	gen temp_fullbalance=.
	bys _caseid (year): replace temp_fullbalance = 1 if !missing(`outcome') & !missing(`outcome'[_n+2]) ///
		& uncontested==0 & uncontested[_n+2]==0 & twoDs_or_twoRs==0 & twoDs_or_twoRs[_n+2]==0 & year==2010
	bys _caseid (year): replace temp_fullbalance = 1 if !missing(`outcome') & !missing(`outcome'[_n-2]) ///
		& uncontested==0 & uncontested[_n-2]==0 & twoDs_or_twoRs==0 & twoDs_or_twoRs[_n-2]==0 & year==2014

	eststo: xi: xtreg `outcome' _aligned _comp i.st_yr [pw=weight] if temp_fullbalance==1 , fe cluster(cluster_dist)
	estadd local indfe "Yes"
	estadd local styrfe "Yes"
}

esttab using "out/tab5.tex", replace nonotes label ///
	title("Reported Campaign Contact" \label{cces-contact}) ///
	mtitles("Any" "In-Person" "Phone" "Mail" "Email/Text") /*nomtitles*/ nostar ///
	scalars("indfe Individual FEs" "styrfe State-Year FEs") b(4) se(4) r2  nogaps drop(_I* _con*)  ///
	addnotes("Standard errors, clustered at the pre/post redistricting CD level, are in parentheses." ///
			 "The dependent variable is coded =1 if the respondent reported campaign contact.")

			 
*Table 4
*** can voters make evaluation about their candidate
set more 1
eststo clear

foreach outcome in _comp_eval _integ_eval _ideo_eval {
	capture drop temp_fullbalance
	gen temp_fullbalance=.
	bys _caseid (year): replace temp_fullbalance = 1 if !missing(`outcome') & !missing(`outcome'[_n+2]) ///
		& uncontested==0 & uncontested[_n+2]==0 & twoDs_or_twoRs==0 & twoDs_or_twoRs[_n+2]==0 & year==2010
	bys _caseid (year): replace temp_fullbalance = 1 if !missing(`outcome') & !missing(`outcome'[_n-2]) ///
		& uncontested==0 & uncontested[_n-2]==0 & twoDs_or_twoRs==0 & twoDs_or_twoRs[_n-2]==0 & year==2014

	eststo: xi: xtreg `outcome' _aligned _comp i.st_yr [pw=weight] if temp_fullbalance==1 , fe cluster(cluster_dist)
	estadd local indfe "Yes"
	estadd local styrfe "Yes"
}

esttab using "out/tab4.tex", replace nonotes label ///
	title("Ability to Evaluate Their Party's Candidate" \label{cces-eval}) ///
	mtitles("Competence" "Integrity" "Ideology") /*nomtitles*/ nostar ///
	scalars("indfe Individual FEs" "styrfe State-Year FEs") b(4) se(4) r2  nogaps drop(_I* _con*)  ///
	addnotes("Standard errors, clustered at the pre/post redistricting CD level," ///
			 "are in parentheses. The dependent variable is coded =1 if the" ///
			 "respondent is able to make evaluation about their party's candidate.")
			 
